package com.defysoft.waukids.intro;

import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.util.Patterns;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.defysoft.waukids.BaseActivity;
import com.defysoft.waukids.R;

public class Intro_FindId_A extends BaseActivity implements OnClickListener {
	EditText editText[] = new EditText[4];
	TextView textView[] = new TextView[2];
	String phoneNumber, email;
	InputMethodManager ipm;		//키보드 올리고 내리기 설정.
/*	TelephonyManager tpManager;	//전화번호국적?설정.
*/	
	private final int FINDPASS_THREAD = 1;
	private final int SUCCESS_MATCH = 9;
	private final int FAIL_MATCH = 10;
	
	Toolbar toolbar;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.intro_findid_a);
		setStartData();
		setStartToolbar();
	}

	@Override
	public void setStartData() {
		super.setStartData();
		ipm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
		editText[0] = (EditText)findViewById(R.id.INTRO_FINDID_EMAIL);
		editText[1] = (EditText)findViewById(R.id.INTRO_FINDID_MOBILE_1);
		editText[2] = (EditText)findViewById(R.id.INTRO_FINDID_MOBILE_2);
		editText[3] = (EditText)findViewById(R.id.INTRO_FINDID_MOBILE_3);
		findViewById(R.id.INTRO_FINDID_BTN).setOnClickListener(this);
		toolbar = (Toolbar)findViewById(R.id.INTRO_FINDID_TOOL);
	}

	private void setStartToolbar() {
		toolbar.setNavigationOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	@Override
	public void startThread(int choice) {
		super.startThread(choice);
		final int cho = choice;
		firstThread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				switch (cho) {
				case FINDPASS_THREAD:	//매치확인스레드.
					checkThread();
					break;

				}
			}
		});
		firstThread.start();
		startDIal();
	}

	protected void checkThread() {	//매치확인스레드.
		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(
					HTTP_MEMBER_FINDPASS_CHECK, "email="+email+"&phoneNumber="+phoneNumber));
			
			if (jsonObject.getBoolean("success")) {
				onNetworkCallback(SUCCESS_MATCH, "");
			} else {
				onNetworkCallback(FAIL_MATCH, jsonObject.getString("message"));
			}			
			
		} catch (JSONException e) {
			onNetworkCallback(CATCH_EXCEPTION, e.toString());
		}
		
	}

	@Override
	protected void handlingMessage(Message _message) {
		super.handlingMessage(_message);
		switch (_message.what) {
		case CATCH_EXCEPTION: // thread 에러 메세지
			endDIal();
			if (firstThread != null) {firstThread.interrupt();}
			Toast.makeText(this,(String) _message.obj, Toast.LENGTH_SHORT).show();
			break;
		
		case SUCCESS_MATCH:
			endDIal();
			if (firstThread != null) {firstThread.interrupt();}		
			//메일?문자? 보내기 작업.
			Toast.makeText(this,"메일로 비밀번호 발송", Toast.LENGTH_SHORT).show();	
			for (int i = 0; i < editText.length; i++) {
				editText[i].setText("");
			}
			break;

		case FAIL_MATCH:
			endDIal();
			if (firstThread != null) {firstThread.interrupt();}	
			Toast.makeText(this,(String) _message.obj, Toast.LENGTH_SHORT).show();			
			break;

		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.INTRO_FINDID_BTN:		//비번찾기 클릭.
			for (int i = 0; i < editText.length; i++) {	//키보드 내리기.
				ipm.hideSoftInputFromWindow(editText[i].getWindowToken(), 0);
			}
			check();	//양식확인
			break;


		}
	}

	private void check() {	//양식확인
		int check = 0;
		email = editText[0].getText().toString();	//메일(아이디)
		if (email.equals("")) {
			Toast.makeText(Intro_FindId_A.this, "아이디(이메일)를 입력하세요.", Toast.LENGTH_SHORT).show();
			editText[0].setHint("아이디(이메일)를 입력하세요.");
			editText[0].setHintTextColor(Color.RED);
			check=1;
		} else if(!isEmailValid(email)){
			Toast.makeText(Intro_FindId_A.this, "이메일 형식이 맞지 않습니다.", Toast.LENGTH_SHORT).show();
			editText[0].setHint("이메일 형식이 맞지 않습니다.");
			check=1;
		}	else if(editText[1].getText().length()<3 ||editText[2].getText().length()<4 ||editText[3].getText().length()<4 ){
		Toast.makeText(Intro_FindId_A.this, "폰번호를 정확하게 입력하세요.", Toast.LENGTH_SHORT).show();
		editText[1].setHintTextColor(Color.RED);
		editText[2].setHintTextColor(Color.RED);
		editText[3].setHintTextColor(Color.RED);
		check=1;
	}
		if (check==0) {
			//!안되면 - http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=1040104&docId=174951622&qb=Z2V0U3lzdGVtU2VydmljZShURUxFUEhPTllfU0VSVklDRSk=&enc=utf8&section=kin&rank=1&search_sort=0&spq=0&pid=SbUPasoRR0osss%2BZlMCsssssssN-191820&sid=VGXeFAoUU0oAAA@EZeo
			email = editText[0].getText().toString();	//메일(아이디)
			phoneNumber = editText[1].getText().toString()+editText[2].getText().toString()+editText[3].getText().toString();
			/*tpManager = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
			phoneNumber = tpManager.getLine1Number();
			if (phoneNumber.contains("+82")) {
				phoneNumber.replace("+82", "0");
			}*/
			startNetwork(FINDPASS_THREAD);	//아이디,폰번호 확인스레드.
			
		}
		
		
	}

	private boolean isEmailValid(String email) {	//메일 양식 체크.
		return Patterns.EMAIL_ADDRESS.matcher(email).matches();
	}

}//CLASS_END
